package frontend.parser.syntaxtree;

import frontend.lexer.TKType;
import frontend.parser.syntaxtree.symbol.NonTerminalSymbol;
import frontend.parser.syntaxtree.symbol.TerminalSymbol;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * ConstDef结点类
 *
 * @author 杨凯伟
 * @date 2024/10/14
 */
public class ConstDefNode extends SyntaxNode {
    public String ident;
    public List<ConstExpNode> dimensions = new ArrayList<>();
    public ConstInitValNode constInitVal;
    public int identLineNum = -1;

    @Override
    public void walk(Consumer<TerminalSymbol> terConsumer, Consumer<NonTerminalSymbol> nonTerConsumer) {
        terConsumer.accept(new TerminalSymbol(TKType.IDENFR, ident));

//        for (var dimension : dimensions) {
//            terConsumer.accept(new TerminalSymbol(TKType.LBRACK));
//
//            dimension.walk(terConsumer, nonTerConsumer);
//
//            terConsumer.accept(new TerminalSymbol(TKType.RBRACK));
//        }

        dimensions.forEach(dimension -> {
            terConsumer.accept(new TerminalSymbol(TKType.LBRACK));
            dimension.walk(terConsumer, nonTerConsumer);
            terConsumer.accept(new TerminalSymbol(TKType.RBRACK));
        });
        terConsumer.accept(new TerminalSymbol(TKType.ASSIGN));
        constInitVal.walk(terConsumer, nonTerConsumer);
        nonTerConsumer.accept(new NonTerminalSymbol(this));
    }

    @Override
    public String getType() {
        return "ConstDef";
    }
}
